package org.heka.imageproc_forfun;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class ImageManipulationsActivity extends Activity implements OnClickListener {
    private static final String TAG             = "Hekafilter::Activity";

    private SoundManager mSoundManager;
    private boolean isSwitchbtn_Clicked = true; 
    
    public static final int     VIEW_MODE_RGBA  = 0;
    public static final int     VIEW_MODE_CANNY = 1;
    public static final int     VIEW_MODE_SEPIA = 2;
    public static final int     VIEW_MODE_SOBEL = 3;
    public static final int     VIEW_MODE_BLUR  = 4;
    public static final int     VIEW_MODE_ZOOM  = 5;
    public static final int     VIEW_MODE_FACE = 6;
    public static final int     VIEW_MODE_BGRA = 7;
    public static final int     VIEW_MODE_SPLIT = 8;
    public static final int     VIEW_MODE_MOZAIK  = 9;
    public static final int     VIEW_MODE_PIXELIZE  = 10;
    public static final int     VIEW_MODE_HIST  = 11;
    public static final int     VIEW_MODE_GRAY  = 12;

    
    
    private static final int    RESULT_COMPLETE = 100;
    public static final int     LISTLOAD_ACTIVITY_CODE  = 101;
    

    public static final int     OVERLAY_NORMAL_IMAGE  = 50;
    public static final int     OVERLAY_MASK_IMAGE  = 53;
    
    public static final int     LIST_NORMAL  = 200;
    public static final int     LIST_MASK  = 201;
    public static final int     LIST_BEARD  = 202;
    public static final int     LIST_GLASSES  = 203;
    public static final int     LIST_HAT  = 204;
    public static final int     LIST_SPECIAL  = 205;
    public static final int     LIST_MOZAIK  = 206;
    public static final int     LIST_HIT = 207;
    public static final int     LIST_CALCULATE  = 208;
    public static final int     LIST_RANDOM  = 209;
    
    public static float         minFaceSize = 0.5f;
    public static int           viewMode        = VIEW_MODE_RGBA;
    public static String mLastPicture = "";    
    public static int overlay_point_ID = OVERLAY_NORMAL_IMAGE;    
    public static int list_category_ID = LIST_NORMAL;
    

    //private MenuItem            mItemPreviewHistogram;
    private MenuItem            mItemPrevieGray;
    private MenuItem            mItemPrevieMozaik;;
    private MenuItem            mItemPrevieblur;;    
    private MenuItem            mItemFPSview;    
    
    ImageView pic_btn;
    ImageButton image1;
    ImageButton image2;
    ImageButton image3;
    ImageButton image4;
    ImageButton image5;
    ImageButton image6;
    ImageButton image7;
    ImageButton image8;
    ImageButton switch_btn;
    ImageButton category_btn;    
    ImageView imgreivew;
    RelativeLayout imgreivew_layout;
    ImageButton discard_btn;
    ImageButton share_btn;
    
    ImageManipulationsView mSurface;
    
    View cameraoption_layout;
    View review_layout;
    View splash_layout;
    
    //Save folder
    String path;
    String mRootPath;
    static final String PICFOLDER = "hekafilter";
    
    Context mMainContext;

    public ImageManipulationsActivity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    
    Resources r;
	Bitmap image1_selelted = null;
	Bitmap image2_selelted = null;
	Bitmap image3_selelted = null;
	Bitmap image4_selelted = null;
	Bitmap image5_selelted = null;
	Bitmap image6_selelted = null;
	Bitmap image7_selelted = null;
	Bitmap image8_selelted = null;
	
	BitmapDrawable drawable1;
	BitmapDrawable drawable2;
	BitmapDrawable drawable3;
	BitmapDrawable drawable4;
	BitmapDrawable drawable5;
	BitmapDrawable drawable6;
	BitmapDrawable drawable7;
	BitmapDrawable drawable8;	
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        
        setContentView(new ImageManipulationsView(this));
        r =  getApplicationContext().getResources();
        
        //addContentView
        LayoutInflater inflater = getLayoutInflater();            
        cameraoption_layout = (View) inflater.inflate(R.layout.camera_option, null);
        addContentView(cameraoption_layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) );        
        review_layout = (View) inflater.inflate(R.layout.review_picture, null);
        addContentView(review_layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) );
        splash_layout = (View) inflater.inflate(R.layout.splash, null);
        addContentView(splash_layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) );
       
        
        
        //splash logo view
    	new Handler().postDelayed(new Runnable() {
			public void run() {
				
				splash_layout.setVisibility(View.GONE);
			}
		}, 5000);
		
        
        
        
        
        pic_btn = (ImageView) findViewById(R.id.btntake);
       
		switch_btn = (ImageButton) findViewById(R.id.switch_btn);
		imgreivew = (ImageView) findViewById(R.id.imgreview);
		imgreivew_layout = (RelativeLayout) findViewById(R.id.review_picture_latout);
		
		image1 = (ImageButton) findViewById(R.id.image_btn1);
		image1.setOnClickListener(this);			
	    image2 = (ImageButton) findViewById(R.id.image_btn2);
	    image2.setOnClickListener(this);		
	    image3 = (ImageButton) findViewById(R.id.image_btn3);
	    image3.setOnClickListener(this);		
	    image4 = (ImageButton) findViewById(R.id.image_btn4);
	    image4.setOnClickListener(this);		
	    image5 = (ImageButton) findViewById(R.id.image_btn5);
	    image5.setOnClickListener(this);		
	    image6 = (ImageButton) findViewById(R.id.image_btn6);
	    image6.setOnClickListener(this);		
	    image7 = (ImageButton) findViewById(R.id.image_btn7);
	    image7.setOnClickListener(this);		
	    image8 = (ImageButton) findViewById(R.id.image_btn8);
	    image8.setOnClickListener(this);		
		discard_btn = (ImageButton) findViewById(R.id.discard_btn);
		discard_btn.setOnClickListener(this);
		share_btn = (ImageButton) findViewById(R.id.share_btn);
		share_btn.setOnClickListener(this);
		category_btn = (ImageButton) findViewById(R.id.category_btn);
		category_btn.setOnClickListener(this);
		
		pic_btn.setOnClickListener(mTakeClick);		
		
		switch_btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	if(isSwitchbtn_Clicked)
            	{
            		SampleCvViewBase.isFrontFacing = true;
            		isSwitchbtn_Clicked = false;
            	}
            	else
            	{
            		SampleCvViewBase.isFrontFacing = false;
            		isSwitchbtn_Clicked = true;
            	}
            	
            }
        });	
			
		mMainContext = this;
		
		// SD card & folder create exception confirm
		String ext = Environment.getExternalStorageState();
		if (ext.equals(Environment.MEDIA_MOUNTED) == false) {
			Toast.makeText(this, "SD card error.", 1).show();
			finish();
			return;
		}		
		mRootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + 
			"/" + PICFOLDER;
		File fRoot = new File(mRootPath);
		if (fRoot.exists() == false) {
			if (fRoot.mkdir() == false) {
				Toast.makeText(this, "folder error.", 1).show();
				finish();
				return;
			}
		}
				
		
		mSoundManager = new SoundManager();
		mSoundManager.initSounds(getBaseContext());
		mSoundManager.addSound(1, R.raw.sound_camera);
		
		
		//이미지 기본컨셉으로 초기화
		image1.setImageResource(R.drawable.normal1_icon);
		image2.setImageResource(R.drawable.normal2_icon);
		image3.setImageResource(R.drawable.normal3_icon);
		image4.setImageResource(R.drawable.normal4_icon);
		image5.setImageResource(R.drawable.normal5_icon);
		image6.setImageResource(R.drawable.normal6_icon);
		image7.setImageResource(android.R.drawable.ic_delete);
		image8.setImageResource(android.R.drawable.ic_delete);
		overlay_point_ID = OVERLAY_NORMAL_IMAGE;
		
		/*image1_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal1);
		image2_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal2);
		image3_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal3);
		image4_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal4);
		image5_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal5);
		image6_selelted = BitmapFactory.decodeResource(r,
				R.drawable.normal6);
		image7_selelted = null;
		image8_selelted = null;			*/
		
		drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal1);
		image1_selelted = drawable1.getBitmap();
		drawable2 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal2);
		image2_selelted = drawable2.getBitmap();
		drawable3 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal3);
		image3_selelted = drawable3.getBitmap();
		drawable4 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal4);
		image4_selelted = drawable4.getBitmap();
		drawable5 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal5);
		image5_selelted = drawable5.getBitmap();
		drawable6 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal6);
		image6_selelted = drawable6.getBitmap();	
		image7_selelted = null;
		image8_selelted = null;	
        
    }
    

	public void onDestroy() {
		super.onDestroy();
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		Bitmap image_for_apply = null;
		
		switch (v.getId()) {

		case R.id.discard_btn:			
			try{
				File file = new File(path);
			    if(file.exists()) 
		        	file.delete();
		    	imgreivew_layout.setVisibility(View.GONE);
				cameraoption_layout.setClickable(true);
				Toast.makeText(mMainContext, "삭제되었습니다.", Toast.LENGTH_LONG).show();
			}
			catch(Exception e)
			{
				 Toast.makeText(mMainContext, "정상적으로 삭제되지 않았습니다.", Toast.LENGTH_LONG).show();
			}			
			break;
			
		case R.id.share_btn:			
			Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);  
			picMessageIntent.setType("image/jpg");  
//			File downloadedPic =  new File(Environment.getExternalStorageDirectory(), "temp.jpg");
			picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(mLastPicture));
			picMessageIntent.putExtra(Intent.EXTRA_SUBJECT, "Playmera Picture");
			picMessageIntent.putExtra(Intent.EXTRA_TEXT, "Let's Playmera!");
			//picMessageIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {});
			startActivity(Intent.createChooser(picMessageIntent, "Playmera"));
			break;
			
		case R.id.category_btn:			
			Intent intent = new Intent(ImageManipulationsActivity.this,
					ListLoad.class);
			startActivityForResult(intent, LISTLOAD_ACTIVITY_CODE);
			break;
			
			//이미지 관련
		case R.id.image_btn1:		     
        	viewMode = VIEW_MODE_FACE;
        	minFaceSize = 0.4f;
            image_for_apply = image1_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            	
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
         
			break;
		case R.id.image_btn2:		
        	viewMode = VIEW_MODE_FACE;
        	minFaceSize = 0.4f;
            image_for_apply = image2_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
    	
			break;
		case R.id.image_btn3:						
        	viewMode = VIEW_MODE_FACE;
        	minFaceSize = 0.4f;
            image_for_apply = image3_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
    		
			break;
		case R.id.image_btn4:			
        	viewMode = VIEW_MODE_FACE;
        	minFaceSize = 0.4f;
            image_for_apply = image4_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.image_btn5:			
			viewMode = VIEW_MODE_FACE;
			minFaceSize = 0.4f;	
            image_for_apply = image5_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.image_btn6:			
			viewMode = VIEW_MODE_FACE;
			minFaceSize = 0.4f;	
            image_for_apply = image6_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.image_btn7:			
			viewMode = VIEW_MODE_FACE;
			minFaceSize = 0.4f;
            image_for_apply = image7_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.image_btn8:			
			viewMode = VIEW_MODE_FACE;
			minFaceSize = 0.4f;
            image_for_apply = image8_selelted;
            if(image_for_apply!=null)
            {
            	ImageManipulationsView.overlay_bitmap = image_for_apply;
            }
            else
            	 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
			break;
			
	
			
		}		
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i(TAG, "onCreateOptionsMenu");
       
        //mItemPreviewHistogram = menu.add("Histogram");
        mItemFPSview = menu.add("FPS on/off");
        mItemPrevieblur = menu.add("Blur");
        mItemPrevieGray = menu.add("Gray");
        mItemPrevieMozaik = menu.add("Mosaik");
        return true;
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i(TAG, "Menu Item selected " + item);
   /*   if (item == mItemPreviewHistogram)
      {
    	  viewMode = VIEW_MODE_HIST;
      }*/
      if (item == mItemFPSview)
      {
    	  if(SampleCvViewBase.isFPSviewOn)
    	  {
    		  SampleCvViewBase.isFPSviewOn = false;
    	  }    		  
    	  else
    	  {
    		  SampleCvViewBase.isFPSviewOn = true;
    	  }    		 
      }
      else if (item == mItemPrevieGray)
      {
    	  viewMode = VIEW_MODE_GRAY;      
      }
      else if (item == mItemPrevieblur)
      {
    	  viewMode = VIEW_MODE_BLUR;
      }
      else if (item == mItemPrevieMozaik)
      {
    	  viewMode =VIEW_MODE_MOZAIK;
      }
     
          
        return true;
    }
    
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode){
	
		//마스크 아이콘 갱신		
		case LISTLOAD_ACTIVITY_CODE:
			if(resultCode == RESULT_COMPLETE){
				list_category_ID = (int) data.getIntExtra("list_category",
						LIST_NORMAL);
				
				Animation anim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, 
						Animation.RELATIVE_TO_SELF, 0.5f);
				anim.setDuration(500);
				image1.startAnimation(anim);
				image2.startAnimation(anim);
				image3.startAnimation(anim);
				image4.startAnimation(anim);
				image5.startAnimation(anim);
				image6.startAnimation(anim);
				image7.startAnimation(anim);
				image8.startAnimation(anim);
			

				if (list_category_ID == LIST_NORMAL) 
				{
				
					image1.setImageResource(R.drawable.normal1_icon);
					image2.setImageResource(R.drawable.normal2_icon);
					image3.setImageResource(R.drawable.normal3_icon);
					image4.setImageResource(R.drawable.normal4_icon);
					image5.setImageResource(R.drawable.normal5_icon);
					image6.setImageResource(R.drawable.normal6_icon);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					
					viewMode = VIEW_MODE_RGBA;
					overlay_point_ID = OVERLAY_NORMAL_IMAGE;
					/*image1_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal1);
					image2_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal2);
					image3_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal3);
					image4_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal4);
					image5_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal5);
					image6_selelted = BitmapFactory.decodeResource(r,
							R.drawable.normal6);
					image7_selelted = null;
					image8_selelted = null;			*/
					drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal1);
					image1_selelted = drawable1.getBitmap();
					drawable2 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal2);
					image2_selelted = drawable2.getBitmap();
					drawable3 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal3);
					image3_selelted = drawable3.getBitmap();
					drawable4 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal4);
					image4_selelted = drawable4.getBitmap();
					drawable5 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal5);
					image5_selelted = drawable5.getBitmap();
					drawable6 = (BitmapDrawable) getResources().getDrawable(R.drawable.normal6);
					image6_selelted = drawable6.getBitmap();	
					image7_selelted = null;
					image8_selelted = null;	
				}
				else if(list_category_ID == LIST_BEARD)
				{
					image1.setImageResource(R.drawable.musta1_icon);
					image2.setImageResource(R.drawable.musta2_icon);
					image3.setImageResource(R.drawable.musta3_icon);
					image4.setImageResource(R.drawable.musta4_icon);
					image5.setImageResource(R.drawable.musta5_icon);
					image6.setImageResource(R.drawable.musta6_icon);
					image7.setImageResource(R.drawable.musta7_icon);
					image8.setImageResource(R.drawable.musta8_icon);
					
					viewMode = VIEW_MODE_RGBA;
					overlay_point_ID = OVERLAY_NORMAL_IMAGE;
					/*image1_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta1);
					image2_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta2);
					image3_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta3);
					image4_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta4);
					image5_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta5);
					image6_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta6);
					image7_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta7);
					image8_selelted = BitmapFactory.decodeResource(r,
							R.drawable.musta8);			*/
					drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta1);
					image1_selelted = drawable1.getBitmap();
					drawable2 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta2);
					image2_selelted = drawable2.getBitmap();
					drawable3 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta3);
					image3_selelted = drawable3.getBitmap();
					drawable4 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta4);
					image4_selelted = drawable4.getBitmap();
					drawable5 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta5);
					image5_selelted = drawable5.getBitmap();
					drawable6 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta6);
					image6_selelted = drawable6.getBitmap();	
					drawable7 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta7);
					image7_selelted = drawable7.getBitmap();	
					drawable8 = (BitmapDrawable) getResources().getDrawable(R.drawable.musta8);
					image8_selelted = drawable8.getBitmap();	
				}
				else if(list_category_ID == LIST_GLASSES)
				{
					image1.setImageResource(R.drawable.glas1_icon);
					image2.setImageResource(R.drawable.glas2_icon);
					image3.setImageResource(R.drawable.glas3_icon);
					image4.setImageResource(R.drawable.glas4_icon);
					image5.setImageResource(R.drawable.glas5_icon);
					image6.setImageResource(R.drawable.glas6_icon);
					image7.setImageResource(R.drawable.glas7_icon);
					image8.setImageResource(R.drawable.glas8_icon);
										
					viewMode = VIEW_MODE_RGBA;
					overlay_point_ID = OVERLAY_NORMAL_IMAGE;
					/*image1_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas1);
					image2_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas2);
					image3_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas3);
					image4_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas4);
					image5_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas5);
					image6_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas6);
					image7_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas7);
					image8_selelted = BitmapFactory.decodeResource(r,
							R.drawable.glas8);		*/
					drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas1);
					image1_selelted = drawable1.getBitmap();
					drawable2 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas2);
					image2_selelted = drawable2.getBitmap();
					drawable3 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas3);
					image3_selelted = drawable3.getBitmap();
					drawable4 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas4);
					image4_selelted = drawable4.getBitmap();
					drawable5 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas5);
					image5_selelted = drawable5.getBitmap();
					drawable6 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas6);
					image6_selelted = drawable6.getBitmap();	
					drawable7 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas7);
					image7_selelted = drawable7.getBitmap();	
					drawable8 = (BitmapDrawable) getResources().getDrawable(R.drawable.glas8);
					image8_selelted = drawable8.getBitmap();	
				}
				else if(list_category_ID == LIST_HAT)
				{
					image1.setImageResource(android.R.drawable.ic_delete);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					viewMode = VIEW_MODE_RGBA;
					 Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
					image1_selelted = null;
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;			
				}
				else if(list_category_ID == LIST_HIT)
				{
					image1.setImageResource(android.R.drawable.ic_delete);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					viewMode = VIEW_MODE_RGBA;
					Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
					image1_selelted = null;
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;			
				}
				else if(list_category_ID == LIST_RANDOM)
				{
					image1.setImageResource(android.R.drawable.ic_delete);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					viewMode = VIEW_MODE_RGBA;
					Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
					image1_selelted = null;
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;			
				}
				else if(list_category_ID == LIST_CALCULATE)
				{					
					image1.setImageResource(android.R.drawable.ic_delete);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					viewMode = VIEW_MODE_RGBA;
					Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
					image1_selelted = null;
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;			
				}
				else if(list_category_ID == LIST_MASK)
				{
					image1.setImageResource(R.drawable.mask1_icon);
					image2.setImageResource(R.drawable.mask2_icon);
					image3.setImageResource(R.drawable.mask3_icon);
					image4.setImageResource(R.drawable.mask4_icon);
					image5.setImageResource(R.drawable.mask5_icon);
					image6.setImageResource(R.drawable.mask6_icon);
					image7.setImageResource(R.drawable.mask7_icon);
					image8.setImageResource(R.drawable.mask8_icon);
					
					viewMode = VIEW_MODE_RGBA;
					overlay_point_ID = OVERLAY_MASK_IMAGE;
					/*image1_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask1);
					image2_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask2);
					image3_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask3);
					image4_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask4);
					image5_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask5);
					image6_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask6);
					image7_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask7);
					image8_selelted = BitmapFactory.decodeResource(r,
							R.drawable.mask8);	*/
					drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask1);
					image1_selelted = drawable1.getBitmap();
					drawable2 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask2);
					image2_selelted = drawable2.getBitmap();
					drawable3 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask3);
					image3_selelted = drawable3.getBitmap();
					drawable4 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask4);
					image4_selelted = drawable4.getBitmap();
					drawable5 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask5);
					image5_selelted = drawable5.getBitmap();
					drawable6 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask6);
					image6_selelted = drawable6.getBitmap();	
					drawable7 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask7);
					image7_selelted = drawable7.getBitmap();	
					drawable8 = (BitmapDrawable) getResources().getDrawable(R.drawable.mask8);
					image8_selelted = drawable8.getBitmap();	
				}
				else if(list_category_ID == LIST_MOZAIK)
				{
					image1.setImageResource(android.R.drawable.ic_delete);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					viewMode = VIEW_MODE_RGBA;
					Toast.makeText(mMainContext, "준비중입니다.", Toast.LENGTH_SHORT).show();
					image1_selelted = null;
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;		
				}
				else if(list_category_ID == LIST_SPECIAL)
				{
					
					image1.setImageResource(R.drawable.ladygaga_icon);
					image2.setImageResource(android.R.drawable.ic_delete);
					image3.setImageResource(android.R.drawable.ic_delete);
					image4.setImageResource(android.R.drawable.ic_delete);
					image5.setImageResource(android.R.drawable.ic_delete);
					image6.setImageResource(android.R.drawable.ic_delete);
					image7.setImageResource(android.R.drawable.ic_delete);
					image8.setImageResource(android.R.drawable.ic_delete);
					
					
					viewMode = VIEW_MODE_RGBA;
					drawable1 = (BitmapDrawable) getResources().getDrawable(R.drawable.ladygaga);
					image1_selelted = drawable1.getBitmap();
					image2_selelted = null;
					image3_selelted = null;
					image4_selelted = null;
					image5_selelted = null;
					image6_selelted = null;
					image7_selelted = null;
					image8_selelted = null;		
				
				}

			}
			break;

		}
	}
    
    Button.OnClickListener mTakeClick = new Button.OnClickListener() {
		public void onClick(View v) {		
			
			//SDcard exception
			String ext = Environment.getExternalStorageState();
	          if (ext.equals(Environment.MEDIA_MOUNTED) == false) {
	              Toast.makeText(mMainContext, "SD 카드가 반드시 필요합니다.", Toast.LENGTH_LONG).show();
	              return;
	          }
	          
	          cameraoption_layout.setClickable(false);
			
			Calendar calendar = Calendar.getInstance();
			String FileName = String.format("hekafilter%02d%02d%02d-%02d%02d%02d.jpg", 
					calendar.get(Calendar.YEAR) % 100, calendar.get(Calendar.MONTH)+1, 
					calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), 
					calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
			path = mRootPath + "/" + FileName;

			//convert bitmap -> byte
			Bitmap bitmap = null;
			Bitmap bmpRotate = null;
			ByteArrayOutputStream stream =null;
			try{							
				bitmap = SampleCvViewBase.CaptureImage; 
						
				if(bitmap != null)
				{
					imgreivew_layout.setVisibility(View.VISIBLE);				
					
					// rotate180 for FrontFacingCamera Saving
					if (SampleCvViewBase.isFrontFacing) {
						Matrix mat = new Matrix();
						mat.setScale(-1, 1);
						bmpRotate = Bitmap.createBitmap(bitmap, 0, 0,
								bitmap.getWidth(), bitmap.getHeight(), mat,
								true);

						stream = new ByteArrayOutputStream();
						bmpRotate.compress(CompressFormat.JPEG, 100, stream);
					}
					// normal - BackCamera for Saving
					else {
						stream = new ByteArrayOutputStream();
						bitmap.compress(CompressFormat.JPEG, 100, stream);
					}

					byte[] capture_data = stream.toByteArray();
					File file = new File(path);
					try {
						FileOutputStream fos = new FileOutputStream(file);
						fos.write(capture_data);
						fos.flush();
						fos.close();
					} catch (Exception e) {
						Toast.makeText(mMainContext,
								"save fail" + e.getMessage(), 0).show();
						return;
					}

					//sound!
					mSoundManager.playSound(1);
					
					
					
					
					//review image!
					/*Bitmap background = Bitmap.createBitmap(
							imgreivew.getWidth(),
							imgreivew.getHeight(), Bitmap.Config.ARGB_8888);
					Canvas canvas = new Canvas(background);*/
					
					/*int newWidth = imgreivew.getWidth();
					int newHeight = imgreivew.getHeight();*/
					int newWidth = 290;
					int newHeight = 220;
					
					
					Matrix mat = new Matrix();
					Bitmap bmp_forReview = null;
					float scaleWidth = ((float) newWidth) / bitmap.getWidth();
					float scaleHeight = ((float) newHeight) / bitmap.getHeight(); 						
					mat.postScale(scaleWidth, scaleHeight);
					
					if (SampleCvViewBase.isFrontFacing)
					{										
						bmp_forReview = Bitmap.createBitmap(bmpRotate, 0, 0,
								bitmap.getWidth(), bitmap.getHeight(), mat,
								true);
						//canvas.drawBitmap(bmpRotate, 0, 0, null);
					}					
					else
					{					    
						bmp_forReview = Bitmap.createBitmap(bitmap, 0, 0,
								bitmap.getWidth(), bitmap.getHeight(), mat,
								true);	
						//canvas.drawBitmap(bitmap, 0, 0, null);
					}				
					//canvas.drawBitmap(bmp_forReview, 0, 0, null);

					//imgreivew.setImageBitmap(background);
					
					
					
					imgreivew.setImageBitmap(bmp_forReview);
					
				/*	if(bmp_forReview!=null)
					{
						bmp_forReview.recycle();
						bmp_forReview = null;
					}
					*/
					
					//cameraoption_layout.setVisibility(View.GONE);
				} else {
					Toast.makeText(mMainContext, "save fail", 0).show();
				}
				
				
			} catch(Exception e) {
				Toast.makeText(mMainContext, "convert fail" + 
						e.getMessage(), 0).show();
			}		
			
			Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
			Uri uri = Uri.parse("file://" + path);
			intent.setData(uri);
			sendBroadcast(intent);
			
			mLastPicture = "file://" + path;			
		
			/*if (SampleCvViewBase.CaptureImage != null) {
				Intent intentPicture = new Intent(ImageManipulationsActivity.this, ReviewPicture.class);
				startActivity(intentPicture);						
			} else {
				Toast.makeText(mMainContext,"Fail",
						Toast.LENGTH_SHORT).show();
			}*/
			
			if(bitmap !=null)	
			{
				//bitmap.recycle();
				bitmap=null;				
			}			
			if(bmpRotate != null)
			{
				//bmpRotate.recycle();				
				bmpRotate = null;
			}
			
		}
	};
	
	
	@Override
	public void onBackPressed() {
		if (imgreivew_layout.getVisibility() == View.VISIBLE) {
			//imgreivew.setVisibility(View.GONE);
			imgreivew_layout.setVisibility(View.GONE);
			cameraoption_layout.setClickable(true);
		} else {
		
			finish();
		}
	}
}


